const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    nianfen: ['请选择年份'],
    yinhang: ['请选择银行'],
    diqu: ['请选择地区'],
    gangwei: ['请选择岗位'],
    phone: 0,
    theList: ''
  },
  bindPickerChange: function (e) {
    let num = e.detail.value;
    let that = this;
    console.log('nianfen发送选择改变，携带值为', that.data.nianfen[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=34451&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'nianfen',
        grtext: that.data.nianfen[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择银行')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].yinhang)
        }
        that.setData({
          yinhang: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let that = this;
    console.log('yinhang发送选择改变，携带值为', that.data.yinhang[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=34451&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'yinhang',
        grtext: that.data.yinhang[num2],
        onefiled: 'nianfen',
        onetext: that.data.nianfen[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择地区')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].diqu)

        }
        that.setData({
          diqu: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let that = this;
    console.log('diqu发送选择改变，携带值为', that.data.diqu[num3]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=34451&callback=?', //接收url
      data: {
        level: '4',
        grfiled: 'diqu',
        grtext: that.data.diqu[num3],
        onefiled: 'nianfen',
        onetext: that.data.nianfen[this.data.index],
        twofiled: 'yinhang',
        twotext: that.data.yinhang[this.data.index2],
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        var list = [];
        list.push('请选择岗位')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].gangwei)
        }
        that.setData({
          gangwei: list
        });
      }
    })
  },
  bindPickerChange4: function (e) {
    let num4 = e.detail.value;
    let that = this;
    console.log('gangwei发送选择改变，携带值为', that.data.gangwei[num4]);
    this.setData({
      index4: e.detail.value
    })
  },

  gg: function (e) {
    wx.navigateTo({
      url: '../gg/gg'
    })
  },

  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/chaxun/getlevel?actid=34451&callback=?', //接收url
      data: {
        level: '1',
        nianfen: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择年份')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].nianfen)
        }
        that.setData({
          nianfen: list
        });
      }
    })
  },


  tijiao: function (res) {
    var that = this;
    var phone = wx.getStorageSync('qzphone');
    var nianfen = that.data.nianfen[that.data.index];
    var yinhang = that.data.yinhang[that.data.index2];
    var diqu = that.data.diqu[that.data.index3];
    var gangwei = that.data.gangwei[that.data.index4];


    if (phone) {
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/writelogs/?actid=34451&callback=?",
        data: {
          phone: phone,
          nianfen: nianfen,
          yinhang: yinhang,
          diqu: diqu,
          gangwei: gangwei,
        },
      })
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/chaxun/getlist/?actid=34451&callback=?",
        data: {
          nianfen: nianfen,
          yinhang: yinhang,
          diqu: diqu,
          gangwei: gangwei,
          limits: '100'
        }, //接收参数
        success(res) { //返回
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          };
          console.log(theJson)
          if (theJson.status == '2') {
            console.log('暂无数据！')
            wx.showToast({
              title: '暂无数据',
              icon: 'none',
              duration: 2000
            })
          } else {
            that.setData({
              theList: theJson.lists
            })
          }

          console.log(that.data.theList)
        }
      })
    } else {
      wx.showToast({
        title: '请先登录',
        duration: 2000,
        icon: 'none'
      })
      that.setData({
        fixed: 1
      })
    }



  },
  getPhoneNumber(e) {
    var that = this;
    var fenxiao = that.data.area;
    var geneal = that.data.geneal;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      wx.setStorageSync('qzphone', res.result)
      console.log(res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/chaxun/register?actid=34054&callback=?', //接收url
        data: {
          phone: res.result,
          fenxiao: fenxiao,
          geneal: geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          that.setData({
            fixed: !1
          })
        }
      })
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/wx_collect_info',
        method: 'POST',
        data: {
          activity_id: 27,
          mobile: res.result,
          area_code: that.data.area,
          agent: that.data.geneal,
          page_index: that.data.page
        },
        success: res => {
          console.log(res);
        }
      })
    }).catch(err => {
      console.error(err);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})